package com.education.basic.ao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.education.basic.ao.DepartmentAo;
import com.education.basic.entity.Department;
import com.education.basic.service.DepartmentService;
import com.education.basic.utils.CodeProvider;
import com.yunshangwoniu.core.baseService.PageService;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.RequestUtils;

@Service("departmentAo")
public class DepartmentAoImpl implements DepartmentAo
{
	@Resource
	private DepartmentService departmentService;
	@Resource
	private PageService<Department> pageService;

	@Override
	public Page<Department> departmentList(int currentPageNum, int pageSize, Map<String, Object> params,HttpServletRequest request)
	{
		Page<Department> page = new Page<Department>(currentPageNum, pageSize,RequestUtils.getPageUrl(request));
		page.setParams(params);
		page = departmentService.queryObject(page, Department.class);
		pageService.htmlBuilder(page, "default");
		
		return page;
	}

	@Override
	public Department newDepartment(Department d)
	{
		d.setCode(CodeProvider.getDepartmentCode());
		return departmentService.saveObject(d);
	}

	@Override
	public void removeDepartment(long departmentId)
	{
		departmentService.deleteObjectById(Department.class, departmentId);
	}

	@Override
	public void update(Department d)
	{
		departmentService.updateObject(d);
	}

	@Override
	public Department findDepartmentById(long id)
	{
		return departmentService.getObjectById(Department.class, id);
	}

	@Override
	public Page<Department> findDepartmentByName(String name,HttpServletRequest request)
	{
		Page<Department> page = new Page<Department>(1,10,RequestUtils.getPageUrl(request));
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("name", name.trim());
		page.setParams(params);
		departmentService.queryObject(page, Department.class);
		pageService.htmlBuilder(page, "default");
		
		return page ;
	}

	@Override
	public void batchDeleteDepartment(List<Long> ids)
	{
		departmentService.batchDeleteObject(ids, Department.class);
	}
}
